//
//  scrollView.m
//  Follow Me
//
//  Created by apple on 2017/3/21.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "scrollView.h"
#import "scrollCollectionViewCell.h"
#define kCellID @"cell"


@interface scrollView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSTimer *cycleTimer;
@end

@implementation scrollView

- (void)setCycleArr:(NSArray *)cycleArr
{
    _cycleArr = cycleArr;
    
    [self.collectionView reloadData];
    
    self.pageControl.numberOfPages = cycleArr.count;
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cycleArr.count * 10 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    [self removeCycleTimer];
    [self addCycleTimer];
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    
    self.autoresizingMask = UIViewAutoresizingNone;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"scrollCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:kCellID];
    
    // 设置collectionView的layout
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    CGSize size = self.collectionView.bounds.size;
    layout.itemSize = size;
}

+ (scrollView *)loadCycleXib
{
    return [[NSBundle mainBundle] loadNibNamed:@"scrollView" owner:nil options:nil].firstObject;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cycleArr.count * 1000;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    scrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    
    if (self.cycleArr.count != 0) {
        
        NSString *jpgStr = self.cycleArr[indexPath.item % self.cycleArr.count];
        cell.bgImage.image = [UIImage imageNamed:jpgStr];
    }
    
    cell.backgroundColor = indexPath.item % 2 == 0 ? [UIColor redColor] : [UIColor yellowColor];
    return cell;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat ofSetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5;
    self.pageControl.currentPage = (int)(ofSetX / scrollView.bounds.size.width) % (self.cycleArr.count);
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeCycleTimer];
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self addCycleTimer];
}



- (void)removeCycleTimer
{
    // 移除定时器
    [self.cycleTimer invalidate];
    self.cycleTimer = nil;
}


// 添加定时器
- (void)addCycleTimer
{
    self.cycleTimer = [NSTimer timerWithTimeInterval:3.0 target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.cycleTimer forMode:NSRunLoopCommonModes];
    
}


- (void)scrollToNext
{
    CGFloat currentOffSetX = self.collectionView.contentOffset.x;
    CGFloat offSetX = currentOffSetX + self.collectionView.bounds.size.width;
    
    [self.collectionView setContentOffset:CGPointMake(offSetX, 0) animated:YES];
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
